﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using System.Threading;
using System.Collections.Specialized;
using Medianamik.Core.Configuration;
using Medianamik.Core.Interfaces;

namespace Medianamik.Core.Security
{
    internal class SecurityProvider : ProviderBase
    {

        #region Fields

        private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

        #endregion

        #region .ctors

        public SecurityProvider()
        {
            Initialize("SecurityProvider", new NameValueCollection());
        }

        #endregion

        #region Events

        #endregion

        #region Properties

        public static SecurityProvider Instance
        {
            get
            {
                return Singleton<SecurityProvider>.Instance;
            }
        }

        private List<InheritedPermission> _allInheritedNodeTypePermissions;
        public List<InheritedPermission> AllInheritedNodeTypePermissions
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allInheritedNodeTypePermissions;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        private List<Permission> _allNodeTypePermissions;
        public List<Permission> AllNodeTypePermissions
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allNodeTypePermissions;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        private List<InheritedPermission> _allInheritedNodePermissions;
        public List<InheritedPermission> AllInheritedNodePermissions
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allInheritedNodePermissions;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        private List<Permission> _allNodePermissions;
        public List<Permission> AllNodePermissions
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allNodePermissions;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        private List<Approbation> _allNodeApprobations;
        public List<Approbation> AllNodeApprobations
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allNodeApprobations;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        private List<Group> _allGroups;
        public List<Group> AllGroups
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allGroups;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        #endregion

        #region Methods

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            LoadData();
        }

        private void LoadData()
        {
            _allInheritedNodeTypePermissions = DALProviderManager.DALProvider.SecurityDALProvider.GetAllNodeTypeInheritedPermissions().ToList();
            _allNodeTypePermissions = DALProviderManager.DALProvider.SecurityDALProvider.GetAllNodeTypePermissions().ToList();
            _allInheritedNodePermissions = DALProviderManager.DALProvider.SecurityDALProvider.GetAllNodeInheritedPermissions().ToList();
            _allNodePermissions = DALProviderManager.DALProvider.SecurityDALProvider.GetAllNodePermissions().ToList();
            _allGroups = DALProviderManager.DALProvider.SecurityDALProvider.GetGroups().ToList();

            if(((MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK)).EnableApprobation)
            {
                _allNodeApprobations = DALProviderManager.DALProvider.SecurityDALProvider.GetAllNodeApprobations().ToList();
            }
            else
            {
                _allNodeApprobations = new List<Approbation>();
            }
        }

        public void Reload()
        {
            NodeTypeManager.Provider.Reload();

            _lock.EnterWriteLock();
            try
            {
                LoadData();
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }

        #endregion
        
    }
}
